import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index'

// console.log(store.state.login.token)

// import VueRouter from 'vue-router'
Vue.use(Router)
import Home from '@/views/home/home'
import login from '@/views/login/login'
import Layout from '@/views/layout/Layout'
import category from '@/components/category'
import product from '@/components/product'
import sub from '@/components/sub.vue'

// import a from '@/views/测试/a.vue'
// import b from '@/views/测试/b.vue'
// //在点击同一个跳转路由的时候，点一下并没有报错，但是多点几下就会报错解决方案
const originalPush = Router.prototype.push

Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
    routes: [{
            path: '/',
            component: Layout,
            redirect: 'home', //重定向
            children: [{
                    path: '/Home',
                    component: Home,
                    Home: '/Home'
                }, //home组件
            ]
        },
        {
            path: '/login',
            component: login,
            name: '/login' //登录
        },
        {
            path: '/category',
            component: category,
            name: '/category', //商品分类

        },
        {
            path: '/product',
            component: product,
            name: '/product' //商品详情

        },
        {
            path: '/sub',
            component: sub,
            name: '/sub' //商品规格
        },
        {
            path: '/cartg',
            component:()=>import('@/components/cartg.vue') ,
            name: 'cartg' //商品规格
        },
        {
            path:'/eabbit',
            component:()=>import('@/components/eabbit.vue') ,
        }
    ],
    // 创建一个路由实例
// const router = new VueRouter({ ... })
 
//调用路由实例对象的beforeEach()方法-声明"全局前置守卫"
//每次发生路由导航跳转的时候，就会自动触发回调函数
})
router.beforeEach(function (to, from, next) {
    // console.log(to)
    // console.log(from)
    if(to.path!=='/login'){
        if(store.state.login.token){
            next()
        }else{
            next('/login')
        }
    }else{
        next()
    }
   
  })
export default router